package gw.com.android.presenter.http;

import android.app.Activity;

import com.tendcloud.appcpa.TalkingDataAppCpa;

import java.io.IOException;

import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.presenter.GTDataResolve;
import gw.com.android.utils.ChannelUtil;
import okhttp3.Request;
import okhttp3.Response;
import www.com.library.app.Logger;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.model.DataItemDetail;
import www.com.library.util.AESCrypto;
import www.com.library.util.AESCrypto2;

/**
 * @author jett
 * @since 2018-12-17.
 */
public class AccountRequest {

    public static void openDemo(final Activity context, String uuid, String mobilePhone, final ReqCallBack<DataItemDetail> callBack) {
        ChannelUtil channelUtil = new ChannelUtil();
        String url = ConfigUtil.instance().getUrlPath(ConfigType.OPEN_DEMO_ACCOUNT);
        Https https = new Https(url);
        try {
            https.addParam("login", AESCrypto2.Encrypt(GTConfig.instance().mCurName));
            https.addParam("uuid", AESCrypto2.Encrypt(uuid));
            https.addParam("mobile", AESCrypto2.Encrypt(mobilePhone));
        } catch (Exception ignore) {
        }
        https.addParam("wx", "app");
        https.addParam("openFrom", "app_android");
        https.addParam("utm_campaign", "open_demo");
        https.addParam("utm_content", "gts2_before");
        String chaneel = channelUtil.getChannel(AppMain.getApp());
        https.addParam("utm_source", chaneel);
        https.addParam("utm_terminal", "android");
        https.post(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                Logger.i("AccountRequest", "on success .result = " + result);
                DataItemDetail itemDetail = GTDataResolve.getInstance().dealOpenDemo(result);
                if (null != itemDetail && itemDetail.getBoolean("isOK")) {
                    if (null != callBack && !context.isFinishing()) {
                        String userid = itemDetail.getString("customerNumber");
                        try {
                            TalkingDataAppCpa.onRegister(AESCrypto.Encrypt(userid));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        callBack.onReqSuccess(itemDetail);
                    }
                } else {
                    if (null != callBack && !context.isFinishing()) {
                        callBack.onReqFailed(itemDetail.getString("msg"));
                    }
                }
            }

            @Override
            public void onFailure(Request request, IOException e) {
                String errorMsg = e == null ? "" : e.getMessage();
                Logger.i("AccountRequest", "on failed : " + errorMsg);
                if (null != callBack && !context.isFinishing()) {
                    callBack.onReqFailed(errorMsg);
                }
            }
        });
    }

}
